package chd.edu.spzx.manager.service.impl;

import chd.edu.spzx.manager.mapper.OrderStatisticsMapper;
import chd.edu.spzx.manager.service.OrderInfoService;
import chd.edu.spzx.model.dto.order.OrderStatisticsDto;
import chd.edu.spzx.model.entity.order.OrderStatistics;
import chd.edu.spzx.model.vo.order.OrderStatisticsVo;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Zhang Chaoliang
 * create 2023
 */
@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    /**
     * 统计查询
     * @param orderStatisticsDto
     * @return
     */
    @Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {
        //根据orderStatisticsDto 查询统计结果数据，返回list集合
        List<OrderStatistics> orderStatisticsList =  orderStatisticsMapper.selectList(orderStatisticsDto);

        //遍历list集合，得到所有日期，把所有日期封装list新集合里面
        List<String> dateList = orderStatisticsList.stream()
                .map(orderStatistics ->
                        DateUtil.format(orderStatistics.getOrderDate(), "yyy-MM-dd")).collect(Collectors.toList());

        //遍历list集合，得到所有日期对应的总金额，把总金额封装到新的list集合里面
        List<BigDecimal> decimalList = orderStatisticsList.stream()
                .map(OrderStatistics::getTotalAmount).collect(Collectors.toList());

        //把两个新的list集合封装到OrderStatisticsVo,返回OrderStatisticsVo
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        orderStatisticsVo.setDateList(dateList);
        orderStatisticsVo.setAmountList(decimalList);
        return orderStatisticsVo;
    }
}
